home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsdps1.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  7.1 KB  |  237 lines

  1. /* Copyright (C) 1991, 1992, 1994, 1996, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsdps1.c,v 1.2 2000/09/19 19:00:28 lpd Exp $ */
  20. /* Display PostScript graphics additions for Ghostscript library */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsmatrix.h"        /* for gscoord.h */
  25. #include "gscoord.h"
  26. #include "gspaint.h"
  27. #include "gxdevice.h"
  28. #include "gxfixed.h"
  29. #include "gxmatrix.h"
  30. #include "gspath.h"
  31. #include "gspath2.h"        /* defines interface */
  32. #include "gzpath.h"
  33. #include "gzcpath.h"
  34. #include "gzstate.h"
  35.  
  36. /*
  37.  * Define how much rounding slop setbbox should leave,
  38.  * in device coordinates.  Because of rounding in transforming
  39.  * path coordinates to fixed point, the minimum realistic value is:
  40.  *
  41.  *      #define box_rounding_slop_fixed (fixed_epsilon)
  42.  *
  43.  * But even this isn't enough to compensate for cumulative rounding error
  44.  * in rmoveto or rcurveto.  Instead, we somewhat arbitrarily use:
  45.  */
  46. #define box_rounding_slop_fixed (fixed_epsilon * 3)
  47.  
  48. /* ------ Graphics state ------ */
  49.  
  50. /* Set the bounding box for the current path. */
  51. int
  52. gs_setbbox(gs_state * pgs, floatp llx, floatp lly, floatp urx, floatp ury)
  53. {
  54.     gs_rect ubox, dbox;
  55.     gs_fixed_rect obox, bbox;
  56.     gx_path *ppath = pgs->path;
  57.     int code;
  58.  
  59.     if (llx > urx || lly > ury)
  60.     return_error(gs_error_rangecheck);
  61.     /* Transform box to device coordinates. */
  62.     ubox.p.x = llx;
  63.     ubox.p.y = lly;
  64.     ubox.q.x = urx;
  65.     ubox.q.y = ury;
  66.     if ((code = gs_bbox_transform(&ubox, &ctm_only(pgs), &dbox)) < 0)
  67.     return code;
  68.     /* Round the corners in opposite directions. */
  69.     /* Because we can't predict the magnitude of the dbox values, */
  70.     /* we add/subtract the slop after fixing. */
  71.     if (dbox.p.x < fixed2float(min_fixed + box_rounding_slop_fixed) ||
  72.     dbox.p.y < fixed2float(min_fixed + box_rounding_slop_fixed) ||
  73.     dbox.q.x >= fixed2float(max_fixed - box_rounding_slop_fixed + fixed_epsilon) ||
  74.     dbox.q.y >= fixed2float(max_fixed - box_rounding_slop_fixed + fixed_epsilon)
  75.     )
  76.     return_error(gs_error_limitcheck);
  77.     bbox.p.x =
  78.     (fixed) floor(dbox.p.x * fixed_scale) - box_rounding_slop_fixed;
  79.     bbox.p.y =
  80.     (fixed) floor(dbox.p.y * fixed_scale) - box_rounding_slop_fixed;
  81.     bbox.q.x =
  82.     (fixed) ceil(dbox.q.x * fixed_scale) + box_rounding_slop_fixed;
  83.     bbox.q.y =
  84.     (fixed) ceil(dbox.q.y * fixed_scale) + box_rounding_slop_fixed;
  85.     if (gx_path_bbox(ppath, &obox) >= 0) {    /* Take the union of the bboxes. */
  86.     ppath->bbox.p.x = min(obox.p.x, bbox.p.x);
  87.     ppath->bbox.p.y = min(obox.p.y, bbox.p.y);
  88.     ppath->bbox.q.x = max(obox.q.x, bbox.q.x);
  89.     ppath->bbox.q.y = max(obox.q.y, bbox.q.y);
  90.     } else {            /* empty path *//* Just set the bbox. */
  91.     ppath->bbox = bbox;
  92.     }
  93.     ppath->bbox_set = 1;
  94.     return 0;
  95. }
  96.  
  97. /* ------ Rectangles ------ */
  98.  
  99. /* Append a list of rectangles to a path. */
  100. int
  101. gs_rectappend(gs_state * pgs, const gs_rect * pr, uint count)
  102. {
  103.     for (; count != 0; count--, pr++) {
  104.     floatp px = pr->p.x, py = pr->p.y, qx = pr->q.x, qy = pr->q.y;
  105.     int code;
  106.  
  107.     /* Ensure counter-clockwise drawing. */
  108.     if ((qx >= px) != (qy >= py))
  109.         qx = px, px = pr->q.x;    /* swap x values */
  110.     if ((code = gs_moveto(pgs, px, py)) < 0 ||
  111.         (code = gs_lineto(pgs, qx, py)) < 0 ||
  112.         (code = gs_lineto(pgs, qx, qy)) < 0 ||
  113.         (code = gs_lineto(pgs, px, qy)) < 0 ||
  114.         (code = gs_closepath(pgs)) < 0
  115.         )
  116.         return code;
  117.     }
  118.     return 0;
  119. }
  120.  
  121. /* Clip to a list of rectangles. */
  122. int
  123. gs_rectclip(gs_state * pgs, const gs_rect * pr, uint count)
  124. {
  125.     int code;
  126.     gx_path save;
  127.  
  128.     gx_path_init_local(&save, pgs->memory);
  129.     gx_path_assign_preserve(&save, pgs->path);
  130.     gs_newpath(pgs);
  131.     if ((code = gs_rectappend(pgs, pr, count)) < 0 ||
  132.     (code = gs_clip(pgs)) < 0
  133.     ) {
  134.     gx_path_assign_free(pgs->path, &save);
  135.     return code;
  136.     }
  137.     gx_path_free(&save, "gs_rectclip");
  138.     gs_newpath(pgs);
  139.     return 0;
  140. }
  141.  
  142. /* Fill a list of rectangles. */
  143. /* We take the trouble to do this efficiently in the simple cases. */
  144. int
  145. gs_rectfill(gs_state * pgs, const gs_rect * pr, uint count)
  146. {
  147.     const gs_rect *rlist = pr;
  148.     gx_clip_path *pcpath;
  149.     uint rcount = count;
  150.     int code;
  151.  
  152.     gx_set_dev_color(pgs);
  153.     if ((is_fzero2(pgs->ctm.xy, pgs->ctm.yx) ||
  154.      is_fzero2(pgs->ctm.xx, pgs->ctm.yy)) &&
  155.     gx_effective_clip_path(pgs, &pcpath) >= 0 &&
  156.     clip_list_is_rectangle(gx_cpath_list(pcpath)) &&
  157.     gs_state_color_load(pgs) >= 0 &&
  158.     (*dev_proc(pgs->device, get_alpha_bits)) (pgs->device, go_graphics)
  159.     <= 1
  160.     ) {
  161.     uint i;
  162.     gs_fixed_rect clip_rect;
  163.  
  164.     gx_cpath_inner_box(pcpath, &clip_rect);
  165.     for (i = 0; i < count; ++i) {
  166.         gs_fixed_point p, q;
  167.         gs_fixed_rect draw_rect;
  168.         int x, y, w, h;
  169.  
  170.         if (gs_point_transform2fixed(&pgs->ctm, pr[i].p.x, pr[i].p.y, &p) < 0 ||
  171.         gs_point_transform2fixed(&pgs->ctm, pr[i].q.x, pr[i].q.y, &q) < 0
  172.         ) {        /* Switch to the slow algorithm. */
  173.         goto slow;
  174.         }
  175.         draw_rect.p.x = min(p.x, q.x) - pgs->fill_adjust.x;
  176.         draw_rect.p.y = min(p.y, q.y) - pgs->fill_adjust.y;
  177.         draw_rect.q.x = max(p.x, q.x) + pgs->fill_adjust.x;
  178.         draw_rect.q.y = max(p.y, q.y) + pgs->fill_adjust.y;
  179.         rect_intersect(draw_rect, clip_rect);
  180.         x = fixed2int_pixround(draw_rect.p.x);
  181.         y = fixed2int_pixround(draw_rect.p.y);
  182.         w = fixed2int_pixround(draw_rect.q.x) - x;
  183.         h = fixed2int_pixround(draw_rect.q.y) - y;
  184.         if (w > 0 && h > 0) {
  185.         if (gx_fill_rectangle(x, y, w, h, pgs->dev_color, pgs) < 0)
  186.             goto slow;
  187.         }
  188.     }
  189.     return 0;
  190.       slow:rlist = pr + i;
  191.     rcount = count - i;
  192.     } {
  193.     bool do_save = !gx_path_is_null(pgs->path);
  194.  
  195.     if (do_save) {
  196.         if ((code = gs_gsave(pgs)) < 0)
  197.         return code;
  198.         gs_newpath(pgs);
  199.     }
  200.     if ((code = gs_rectappend(pgs, rlist, rcount)) < 0 ||
  201.         (code = gs_fill(pgs)) < 0
  202.         )
  203.         DO_NOTHING;
  204.     if (do_save)
  205.         gs_grestore(pgs);
  206.     else if (code < 0)
  207.         gs_newpath(pgs);
  208.     }
  209.     return code;
  210. }
  211.  
  212. /* Stroke a list of rectangles. */
  213. /* (We could do this a lot more efficiently.) */
  214. int
  215. gs_rectstroke(gs_state * pgs, const gs_rect * pr, uint count,
  216.           const gs_matrix * pmat)
  217. {
  218.     bool do_save = pmat != NULL || !gx_path_is_null(pgs->path);
  219.     int code;
  220.  
  221.     if (do_save) {
  222.     if ((code = gs_gsave(pgs)) < 0)
  223.         return code;
  224.     gs_newpath(pgs);
  225.     }
  226.     if ((code = gs_rectappend(pgs, pr, count)) < 0 ||
  227.     (pmat != NULL && (code = gs_concat(pgs, pmat)) < 0) ||
  228.     (code = gs_stroke(pgs)) < 0
  229.     )
  230.     DO_NOTHING;
  231.     if (do_save)
  232.     gs_grestore(pgs);
  233.     else if (code < 0)
  234.     gs_newpath(pgs);
  235.     return code;
  236. }
  237.